package com.wenc.psi.dao;

import androidx.lifecycle.LiveData;

import com.wenc.psi.pojo.SaleDetail;
import com.wenc.psi.utils.JDBCUtils;
import com.wenc.psi.pojo.Sale;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SaleDao extends JDBCUtils implements Dao<Sale> {
    @Override
    public void insert(Sale... sales) {
        try {
            conn = getConnection();
            stmt = conn.createStatement();
            for (int i = 0; i < sales.length; i++) {
                String sql = "INSERT INTO sale VALUES('"
                        + sales[i].getId() + "','"
                        + sales[i].getCustomerID() + "','"
                        + sales[i].getEmployeeID() + "','"
                        + sales[i].getDate() + "','"
                        + sales[i].getTime() + "','"
                        + sales[i].getDiscount() + "','"
                        + sales[i].getAddition() + "','"
                        + sales[i].getTotal() + "','"
                        + sales[i].getPaymentStatus() + "','"
                        + sales[i].getPaymentMethod() + "','"
                        + sales[i].getStatisticStatus() + "','"
                        + sales[i].getNote() + "','"
                        + sales[i].getStatus() + "')";
                stmt.executeUpdate(sql);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(stmt, conn);
        }
    }

    public void insertOrder(Sale sale, SaleDetail... saleDetails) {
        try {
            conn = getConnection();
            conn.setAutoCommit(false);
            stmt = conn.createStatement();
            String sql = "INSERT INTO sale VALUES('"
                    + sale.getId() + "','"
                    + sale.getCustomerID() + "','"
                    + sale.getEmployeeID() + "','"
                    + sale.getDate() + "','"
                    + sale.getTime() + "','"
                    + sale.getDiscount() + "','"
                    + sale.getAddition() + "','"
                    + sale.getTotal() + "','"
                    + sale.getPaymentStatus() + "','"
                    + sale.getPaymentMethod() + "','"
                    + sale.getStatisticStatus() + "','"
                    + sale.getNote() + "','"
                    + sale.getStatus() + "')";
            stmt.executeUpdate(sql);
            for (int i = 0; i < saleDetails.length; i++) {
                sql = "INSERT INTO saledetail VALUES('"
                        + saleDetails[i].getSaleId() + "','"
                        + saleDetails[i].getSerialNumber() + "','"
                        + saleDetails[i].getPartId() + "','"
                        + saleDetails[i].getPrice() + "','"
                        + saleDetails[i].getQuantity() + "')";
                stmt.executeUpdate(sql);
            }

            conn.commit();
        } catch (SQLException throwables) {
            // 回滚
            try {
                if (conn != null) conn.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            throwables.printStackTrace();
        } finally {
            close(stmt, conn);
        }
    }

    @Override
    public void delete(Sale... sales) {

    }

    @Override
    public void delete(String... ids) {

    }

    @Override
    public void update(Sale oldT, Sale newT) {

    }

    @Override
    public List<Sale> getAllData() {
        List<Sale> saleList = new ArrayList<>();
        try {
            conn = getConnection();
            String sql = "SELECT * FROM sale";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Sale sale = new Sale();
                sale.setId(rs.getString(1));
                sale.setCustomerID(rs.getString(2));
                sale.setEmployeeID(rs.getString(3));
                sale.setDate(rs.getDate(4));
                sale.setTime(rs.getTime(5));
                sale.setDiscount(rs.getDouble(6));
                sale.setAddition(rs.getDouble(7));
                sale.setTotal(rs.getDouble(8));
                sale.setPaymentMethod(rs.getString(9));
                sale.setPaymentStatus(rs.getString(10));
                sale.setStatisticStatus(rs.getString(11));
                sale.setNote(rs.getString(12));
                sale.setStatus(rs.getString(13));
                saleList.add(sale);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(rs, pstmt, conn);
        }
        return saleList;
    }

    @Override
    public Sale getDataById(String id) {
        return null;
    }

    @Override
    public LiveData<List<Sale>> getAllLiveData() {
        return null;
    }

}
